package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class NursingProjectServiceimpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(pageNum,pageSize);
        //调用方法查看数据
        Page<NursingProject> page = nursingProjectMapper.selectByPage(name, status);
        //封装返回后的结果
        return PageResponse.of(page, NursingProjectVo.class);
    }

    /**
     * 新增护理项目
     * @param nursingProjectDTO 护理项目数据传输对象
     */
    @Override
    public void add(NursingProjectDto nursingProjectDTO) {
        NursingProject nursingProject = new NursingProject();
        BeanUtil.copyProperties(nursingProjectDTO,nursingProject);
        nursingProject.setCreateTime(LocalDateTime.now());
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProjectMapper.insert(nursingProject);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo getById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.selectById(id);
       return BeanUtil.toBean(nursingProject, NursingProjectVo.class);
    }

    /**
     * 更新护理项目
     * @param nursingProjectDto
     */
    @Override
    public void update(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtil.copyProperties(nursingProjectDto,nursingProject);
        nursingProject.setCreateTime(LocalDateTime.now());
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProjectMapper.update(nursingProject);
    }

    /**
     *删除护理项目
     * @param id
     */
    @Override
    public void delete(Long id) {
        nursingProjectMapper.delete(id);
    }

    /**
     * 启用和禁用
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, int status) {
        nursingProjectMapper.updateStatus(id,status);
    }

    /**
     * 查询所有护理项目
     * @return
     */
    @Override
    public List<NursingProject> selectAll() {
        return nursingProjectMapper.selectAll();
    }

}
